<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form 
          v-loading="showLoading"
          element-loading-text="上传中"
          element-loading-spinner="el-icon-loading"
          ref="form" 
          :model="form" 
          :rules="rules" 
          size="small" 
          label-width="80px"
        >
          <el-form-item label="状态">
            <!-- <el-input v-model="form.status" style="width: 370px;" /> -->
            <el-select v-model="form.status" placeholder="请选择" style="width: 370px;">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
<!--          <el-form-item label="类别">
            <el-input v-model="form.type" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item label="视频链接">
            <div style="display:flex;width:370px">
              <el-input v-model="form.vedio" style="margin-right: 5px" />
              <el-upload
                ref="upload"
                :limit="1"
                :headers="headers"
                :show-file-list="false"
                :before-upload="beforeUpload"
                :on-success="handleSuccess"
                :on-error="handleError"
                :action="baseApi + '/front/upload/uploadV1'"
              >
                <el-button type="primary">上传视频</el-button>
              </el-upload>
            </div>
            <video v-if="form.vedio" :src="form.vedio" controls style="width: 370px; height:210px"/>
          </el-form-item>
          <el-form-item label="视频缩略图">
            <div style="display:flex;width:370px">
              <el-input v-model="form.vedioSuoluetu" style="margin-right: 5px" />
              <el-upload
                ref="upload1"
                :limit="1"
                :headers="headers"
                :show-file-list="false"
                :before-upload="beforeUpload1"
                :on-success="handleSuccess1"
                :on-error="handleError"
                :action="baseApi + '/front/upload/uploadV1'"
              >
                <el-button type="primary">上传图片</el-button>
              </el-upload>
            </div>
            <el-image v-if="form.vedioSuoluetu" :src="form.vedioSuoluetu" style="width: 150px;height:150px;"></el-image>
          </el-form-item>
          <el-form-item label="简介">
            <el-input v-model="form.jainjie" style="width: 370px;" type="textarea"/>
          </el-form-item>
          <el-form-item label="排序">
            <el-input v-model="form.num" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="标题">
            <el-input v-model="form.title" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="内容">
            <el-input v-model="form.content" style="width: 370px;" type="textarea" :autosize="{ minRows: 2, maxRows: 10}"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2 || showLoading" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="id" />
        <el-table-column prop="status" label="状态" >
          <template slot-scope="scope">
            {{scope.row.status=='1' ? "正常" : scope.row.status=='0' ? '异常' : '错误'}}
          </template>
        </el-table-column>
       <!-- <el-table-column prop="type" label="类别" /> -->
        <!-- <el-table-column prop="vedio" label="视频链接">
          <div slot-scope="scope" @click="showVideo(scope.row.vedio)">
            {{ scope.row.vedio }}
          </div>
        </el-table-column> -->
        <el-table-column prop="vedioSuoluetu" label="视频缩略图">
          <div slot-scope="scope" >
            <el-image v-if="scope.row.vedioSuoluetu" :src="scope.row.vedioSuoluetu" @click="showVideo(scope.row.vedio)"></el-image>
          </div>
        </el-table-column>
        <el-table-column prop="jainjie" label="简介" />
        <el-table-column prop="num" label="排序" />
        <el-table-column prop="createdAt" label="创建时间" />
        <el-table-column prop="updatedAt" label="修改时间" />
        <el-table-column prop="title" label="标题" />
        <!-- <el-table-column prop="content" label="内容" /> -->
        <el-table-column v-if="checkPer(['admin','dccKpsp:edit','dccKpsp:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <el-dialog v-if="dialogVisible" :visible.sync="dialogVisible" title="视频">
      <video :src="videoUrl" controls style="width:100%"></video>
    </el-dialog>
  </div>
</template>

<script>
import crudDccKpsp from '@/api/dccKpsp'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'

const defaultForm = { id: null, status: null, type: null, vedio: null, vedioSuoluetu: null, jainjie: null, operator: null, num: null, createdAt: null, updatedAt: null, title: null, content: null }
export default {
  name: 'DccKpsp',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '科普视频', url: 'api/dccKpsp', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDccKpsp }})
  },
  data() {
    return {
      headers: { 'Authorization': getToken() },
      options: [{
        value: 1,
        label: '正常'
      }, {
        value: 0,
        label: '异常'
      }],
      videoUrl: '',
      dialogVisible: false,
      permission: {
        add: ['admin', 'dccKpsp:add'],
        edit: ['admin', 'dccKpsp:edit'],
        del: ['admin', 'dccKpsp:del']
      },
      showLoading: false,
      rules: {
      }    }
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    beforeUpload (file) {
      let list = ['avi','wmv','mp4','mpeg','m4v','mov','asf','flv','f4v','rmvb','rm','3gp','vob']
      let type = file.type.split('/')[1] ? file.type.split('/')[1] : ''
      if (list.indexOf(type) === -1) {
        this.$message.error('文件类型不正确')
        return false
      }
      this.showLoading = true
    },
    handleSuccess (response,file,fileList) {
      if (response && response.imgUrl) {
        this.form.vedio = response.imgUrl
        this.$refs.upload.clearFiles()
        this.showLoading = false
        this.$message.success('上传成功')
      }
    },
    beforeUpload1 (file) {
      let list = ['jpeg','png','jpeg','dds','psd','pdt','webp','xmp','gif','bmp','svg','tiff']
      let type = file.type.split('/')[1] ? file.type.split('/')[1] : ''
      if (list.indexOf(type) === -1) {
        this.$message.error('文件类型不正确')
        return false
      }
      this.showLoading = true
    },
    handleSuccess1 (response,file,fileList) {
      if (response && response.imgUrl) {
        this.form.vedioSuoluetu = response.imgUrl
        this.$refs.upload1.clearFiles()
        this.showLoading = false
        this.$message.success('上传成功')
      }
    },
    handleError () {
        this.showLoading = false
        this.$message.error('上传失败')
    },
    showVideo(url) {
      this.videoUrl = url
      this.dialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container >>> .el-dialog__body {
  padding-top: 0; 
  text-align: center;
  video {
    height: 450px;
  }
}
</style>
